package com.hongxinguoji.manager.content.service.imp;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongxinguoji.manager.content.mapper.HxContentMapper;
import com.hongxinguoji.manager.content.pojo.HxContent;
import com.hongxinguoji.manager.content.pojo.HxContentExample;
import com.hongxinguoji.manager.content.pojo.HxContentExample.Criteria;
import com.hongxinguoji.manager.content.service.IContentService;

@Service
public class ContentServiceImpl implements IContentService {

	@Autowired
	private HxContentMapper contentMapper;

	@Override
	public List<HxContent> getContentList(Long categoryId) {
		HxContentExample example = new HxContentExample();

		// 如果有内容分类，则根据内容分类查询
		if (categoryId != null) {
			Criteria criteria = example.createCriteria();
			criteria.andCidEqualTo(categoryId);
		}

		List<HxContent> list = contentMapper.selectByExample(example);
		return list;
	}

	@Override
	public void saveContent(HxContent content) {

		// 补全日期属性
		Date date = new Date();

		content.setCreated(date);
		content.setUpdated(date);

		// 设置状态 1：可用，0：删除
		content.setStatus(1);

		// 插入数据
		contentMapper.insert(content);
	}

	@Override
	public void updateContent(HxContent content) {

		Long id = content.getId();
		HxContent hxContent = contentMapper.selectByPrimaryKey(id);

		hxContent.setPic(content.getPic());
		hxContent.setSortid(content.getSortid());
		hxContent.setSubTitle(content.getSubTitle());
		hxContent.setTitle(content.getTitle());
		hxContent.setTitleDesc(content.getTitleDesc());
		hxContent.setUpdated(new Date());
		hxContent.setUrl(content.getUrl());

		// 更新操作
		contentMapper.updateByPrimaryKey(hxContent);

	}

	@Override
	public void lockContent(Long i) {
		HxContent content = contentMapper.selectByPrimaryKey(i);
		// 状态不可用：0
		content.setStatus(0);

		contentMapper.updateByPrimaryKey(content);

	}

	@Override
	public void unlockContent(Long i) {
		HxContent content = contentMapper.selectByPrimaryKey(i);
		// 状态可用：1
		content.setStatus(1);
		contentMapper.updateByPrimaryKey(content);

	}

	@Override
	public void deleteContent(Long i) {
		contentMapper.deleteByPrimaryKey(i);
	}
}